from sqlalchemy import Column, Integer, String, Float, Date, Text, JSON
from sqlalchemy.ext.declarative import declarative_base
from datetime import datetime

Base = declarative_base()

class ShapAnalysis(Base):
    """SHAP可解释性分析表"""
    __tablename__ = 'shap_analysis'
    
    id = Column(Integer, primary_key=True, autoincrement=True)
    model = Column(String(32), nullable=False, comment='模型名称')
    train_dt = Column(Date, nullable=False, comment='训练日期')
    
    # 特征类别贡献度
    historical_load_contribution = Column(Float, nullable=True, comment='历史负荷贡献比例')
    weather_contribution = Column(Float, nullable=True, comment='气象因素贡献比例')
    time_contribution = Column(Float, nullable=True, comment='时间因素贡献比例')
    
    # 具体气象因素重要性
    temp_max_importance = Column(Float, nullable=True, comment='最高温度重要性')
    temp_min_importance = Column(Float, nullable=True, comment='最低温度重要性')
    precipitation_importance = Column(Float, nullable=True, comment='降水重要性')
    humidity_importance = Column(Float, nullable=True, comment='湿度重要性')
    
    # 时间因素重要性
    weekday_importance = Column(Float, nullable=True, comment='工作日重要性')
    month_importance = Column(Float, nullable=True, comment='月份重要性')
    holiday_importance = Column(Float, nullable=True, comment='节假日重要性')
    
    # 可解释性分析文本
    historical_load_impact = Column(Text, nullable=True, comment='历史负荷影响分析')
    weather_impact = Column(Text, nullable=True, comment='气象因素影响分析')
    weather_detail = Column(Text, nullable=True, comment='气象因素详细分析')
    time_impact = Column(Text, nullable=True, comment='时间因素影响分析')
    time_detail = Column(Text, nullable=True, comment='时间因素详细分析')
    holiday_impact = Column(Text, nullable=True, comment='节假日影响分析')
    
    # 完整的特征重要性JSON数据
    feature_importance_json = Column(JSON, nullable=True, comment='完整特征重要性数据JSON')
    
    created_at = Column(Date, nullable=True, default=datetime.now, comment='创建时间')
    
    def __repr__(self):
        return f"<ShapAnalysis(model='{self.model}', train_dt='{self.train_dt}')>"